const app = getApp();

Page({
  data: {
    currentTab: 0,
    orders:[],
    order:{},
    unpay:[],
    payed:[],
    finished:[],
  },
  onLoad(query) {
    // 页面加载
    //console.log(app.userId)
    // 重置数据
    this.setData({
      orders:[],
      order:{},
      unpay:[],
      payed:[],
      finished:[],
    })
    this.sendOrderQueryRequest();
    my.showLoading({
        content: '加载中...',
        delay: 1000,
      });
      setTimeout(() => {
        my.hideLoading();
      }, 3000);
    },
  // onShow() {
  //   this.onLoad();
  // },
  //点击切换
  clickTab: function (e) {
    const that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //滑动切换
  swiperTab: function (e) {
    const that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  setStorageSync() {
    var key = `carNum`;
    var data = this.data.carNum;
    if (key.length === 0) {
      this.setData({
        keySync: key,
        dataSync: data,
      });
    } else {
      let res = my.setStorageSync({ key: key, data: data });
      if (!res.error) {
        this.setData({
          keySync: key,
          dataSync: data,
        });
      }
    }
  },
  goToDetailPage(e) {
    var orderNo = e.currentTarget.id;
    my.navigateTo({
      url: '/page/check-order-detail/check-order-detail?orderNo='+orderNo
    })
    
  },
  sendOrderQueryRequest() {
    my.httpRequest({
      url: app.findOrders_url,
      data:{
        uid: app.userId,
      },
      method: 'GET',
      success: (res) => {
        //console.log(res)
        if(res.data.status===0){
          this.setData({
            status: res.data.status,
          })
          return;
        }
        for(var i=0;i<res.data.data.length;i++){
          var orderNo = res.data.data[i].order_sn;
          var status = res.data.data[i].status;
          var orderStatus = '';
          var carNum = res.data.data[i].car_num;
          var payTotal = res.data.data[i].order_amount;
          var process = res.data.data[i].process;
          var orderTime = res.data.data[i].times;
          var order = this.data.order;
          var orders = this.data.orders;
          // 0 未支付 1 已支付 2 支付异常 23 准备退款 24 退款中 25 退款成功 26 退款异常
          switch(status){
            case '0':
              orderStatus = '等待确认';
              break;
            case '1':
              orderStatus = '支付成功';
              break;
            case '2':
              orderStatus = '支付异常';
              break;
            case '23':
              orderStatus = '准备退款';
              break;
            case '24':
              orderStatus = '退款中';
              break;
            case '25':
              orderStatus = '退款成功';
              break;
            case '26':
              orderStatus = '退款异常';
              break;
          }
          this.setData({
            'order.orderNo': orderNo,
            'order.carNum': carNum,
            'order.payTotal': payTotal,
            'order.process': process,
            'order.orderTime': orderTime,
            'order.orderStatus': orderStatus,
            'order.status': status,
          })
          if(process==='0'){
            //未支付订单
            var unpayOrders = this.data.unpay;
            unpayOrders.push(this.data.order);
            this.setData({
              unpay: unpayOrders,
            })
          }else if(process==='27' || process==='25' || process==='28'){
            //已完成订单
            var finishedOrders = this.data.finished;
            finishedOrders.push(this.data.order);
            this.setData({
              finished: finishedOrders,
            })
          }else{
            //已支付订单
            var payedOrders = this.data.payed;
            payedOrders.push(this.data.order);
            this.setData({
              payed: payedOrders,
            })
          }
          // orders = this.data.orders;
          // orders[i]=this.data.order;
          // this.setData({
          //     orders: orders,
          // })
        }
        //console.log(this.data)
        
      },
      fail: function(res) {
        //console.log({content: '订单查询失败'});
      },
      complete: function(res) {
        
      }
    })
    
  },
  onPullDownRefresh() {
    this.onLoad();
    setTimeout(function(){
      my.stopPullDownRefresh();
    }, 2000);  
  },

});
